package com.wowudi.controller;

import com.wowudi.dto.Result;
import com.wowudi.entity.Menu;
import com.wowudi.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "菜单管理")
@RequestMapping("api/system/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @ApiOperation(value = "返回所有顶级菜单以及下的子菜单")
    @GetMapping("getMenu")
    public Result getMenu(){
        Result result = menuService.getMenu();
        return result;
    }


    @ApiOperation(value = "添加菜单")
    @PostMapping("addMenu")
    public Result addMenu(@RequestBody Menu menu){
        int i = menuService.addMenu(menu);
        Result result = new Result();
        if (i==1){
            result.setMessage("添加菜单成功!");
        }
        else{
            result.setMessage("系统繁忙！");
            result.setCode(500);
        }
        return result;
    }


    @ApiOperation(value = "删除菜单以及子菜单")
    @PostMapping("deleteMenu")
    public Result deleteMenu(@RequestBody Menu menu){
        System.out.println(menu);
        int i = menuService.deleteMenu(menu);
        Result result = new Result();
        if (i==2){
            result.setMessage("删除菜单成功!");
        }
        else{
            result.setMessage("系统繁忙！");
            result.setCode(500);
        }
        return result;
    }


    @ApiOperation(value = "修改菜单")
    @PostMapping("updateMenu")
    public Result updateMenu(@RequestBody Menu menu){
        System.out.println(menu);
        int i = menuService.updateMenu(menu);
        Result result = new Result();
        if (i==2){
            result.setMessage("修改菜单成功!");
        }
        else{
            result.setMessage("系统繁忙！");
            result.setCode(500);
        }
        return result;
    }
}
